package cn.code;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


public class CodeGenerator {

    public static void main(String[] args){

        AutoGenerator mybatisGenerator = new AutoGenerator();
        DataSourceConfig dataSourceConfig = setMybatisDataSourceConfig();
        StrategyConfig strategyConfig = setMybatisStrategyConfig();
        PackageConfig packageConfig = setMybatisPackageConfig();
        GlobalConfig globalConfig = setMybatisGlobalConfig();

        mybatisGenerator.setDataSource(dataSourceConfig);
        mybatisGenerator.setGlobalConfig(globalConfig);
        mybatisGenerator.setPackageInfo(packageConfig);
        mybatisGenerator.setStrategy(strategyConfig);
        mybatisGenerator.execute();
    }


    protected static GlobalConfig setMybatisGlobalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir("E:\\code");
        globalConfig.setFileOverride(true);
        globalConfig.setActiveRecord(true);
        globalConfig.setEnableCache(false);
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setAuthor("chenzhuo_m");

        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        globalConfig.setXmlName("%sMapper");
        return globalConfig ;
    }

    protected static DataSourceConfig setMybatisDataSourceConfig(){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setTypeConvert(new MySqlTypeConvert() {
            @Override// 自定义数据库表字段类型转换【可选】
            public DbColumnType processTypeConvert(String fieldType) {
                System.out.println("转换类型：" + fieldType);
                return super.processTypeConvert(fieldType);
            }
        });
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/lombok-demo");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("1234");
        return dataSourceConfig ;
    }

    protected static StrategyConfig setMybatisStrategyConfig(){
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setTablePrefix(new String[]{"cy_"});
        strategy.setInclude(new String[]{"cy_user"});

        strategy.setSuperMapperClass(null);
        strategy.setSuperServiceClass(null);
        strategy.setSuperServiceImplClass(null);

        return strategy ;
    }

    protected static PackageConfig setMybatisPackageConfig(){
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.carl");
        packageConfig.setEntity("model");
        packageConfig.setController("controller");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setService("service");
        packageConfig.setMapper("mapper");
        packageConfig.setXml("mapper");
        return packageConfig ;
    }

}
